import requests
from queue import Queue
from bs4 import BeautifulSoup
import re
import gevent
import time

class Spider:
    def __init__(self):
        self.headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}

        self.base_url='https://www.kuaikanmanhua.com/tag/0?state=1&sort=1&page={}'

        self.dataQueue=Queue()

        #self.count=0

        self.manhuas=[]
#获取一页数据的方法
    def get_page_manhuas(self,url):
        content = requests.get(url, headers=self.headers).content

        soup = BeautifulSoup(content, 'html5lib')
        #manhua_list =  tagContent.find_all('div', class_='ItemSpecial fl')
        tagContent= soup.find('div', class_='tagContent cls')
        #pattern = re.compile(r'ItemSpecial fl(.)')
        #manhua_list =  tagContent.find_all('div', class_=pattern)
        manhua_list = tagContent.find_all('div',class_='ItemSpecial fl')

        for item in manhua_list:
            manhua = {}
            #名字
            manhua['title'] = item.find('span', class_='itemTitle').text
            #作者
            manhua['author'] =item.find('span', class_='author fl').text
            #点赞量
            manhua['dianzan'] = item.find('span',class_='zanNumber fr').find('span',class_='fl').text
            self.dataQueue.put(manhua)
    def start_work(self,pageNum):
        job_list=[]
        for page in range(1,pageNum+1):
            url=self.base_url.format(page)
        #创建协程任务
            job=gevent.spawn(self.get_page_manhuas,url)
        #把所有协程任务加入列表中
            job_list.append(job)
            gevent.joinall(job_list)
        while not self.dataQueue.empty():
          manhua=self.dataQueue.get()
          self.manhuas.append(manhua)
if __name__=="__main__":
    pages=int(input('请输入多少页:'))
    t1=time.time()
    spider=Spider()
    spider.start_work(pages)
    print(len(spider.manhuas),spider.manhuas[-1])
    t2=time.time()
    print(t2-t1)

